/* ************************************************************
	File:        /fabridge_example02/assets/js/fabridge_example02.js
	Author:      Brice Mason
	Description: Used to run the various demonstrations of the FABridge example.
	
	Revision History:
		2007-06-13 bmason - Creation date.
		
	Copyright (c) 2007 by Brice Mason
	http://www.skipslate.com
	No warranty. Commerical and non-commerical use freely permitted.
************************************************************ */
// the swf
var flexApp;

/* ============================================================
	Function:    initCallback
	Description: Function registered as a callback after the swf
	is finished loading. We use this as a means to initialize the 
	reference to the embedded Flex application.
============================================================ */
// define callback function, we use it to build the bridge on load
var initCallback = function() {
	flexApp = FABridge.flex.root();
	return;
}
// register the callback to our Flex application with the 'bridgeName' of 'flex'
FABridge.addInitializationCallback( "flex", initCallback );
// ---------- initCallback ------------------------------------

/* ============================================================
	Function:    initDemo
	Description: Used to demonstrate key capabilities of the FABridge.
============================================================ */
function initDemo() {
	var str_msg       = "";
	
	// ++++++++++ 'get' demonstration ++++++++++
	// get swf object by id
	// when using the FABridge, this is accomplished through functions.
	var swfObj_firstName = flexApp.getTxt_firstName();
	var swfObj_lastName  = flexApp.getTxt_lastName();
	// get value in the 'text' attribute.
	// same thing, the FABridge uses functions
	var txt_firstName = swfObj_firstName.getText();
	var txt_lastName  = swfObj_lastName.getText();
	// output the results obtained from the swf controls.
	str_msg += "Using the FABridge, we can access objects within the embedded SWF using their id.\n";
	str_msg += "We can also access properties of these objects:\n\n";
	str_msg += "First Name: " + txt_firstName + "\n";
	str_msg += "Last Name: "  + txt_lastName;
	alert( str_msg );
	
	// ++++++++++ 'set' demonstration ++++++++++
	// we can 'get' now let's 'set' some properties
	str_msg =  "Just as we can 'get' properties, we can also 'set' in the same fashion.\n";
	str_msg += "The first name will now be 'Over' and the last name will now be 'Ridden'.";
	alert( str_msg );
	swfObj_firstName.setText( "Over" );
	swfObj_lastName.setText( "Ridden" );
	
	// ++++++++++ object method demonstration ++++++++++
	// now let's call some object methods.
	str_msg =  "We can also call object methods just as if we were using ActionScript.\n";
	str_msg += "First, we bring focus to the 'Last Name' in the swf, then we'll select some text.";
	alert( str_msg );
	swfObj_lastName.setFocus();
	swfObj_lastName.setSelection( 1, 4 );
	
	// ++++++++++ function pass demonstration ++++++++++
	// now let's pass a function in the form of an event listener to ActionScript
	// we'll attach an event listener to the 'Last Name' control in the swf.
	// as data changes in the 'Last Name' control of the swf, it updates the value in the
	// 'Last Name' control in the HTML form.
	str_msg =  "Functions may also be passed to ActionScript.\n";
	str_msg += "We will now attach an event listener to the 'Last Name' control in the SWF.\n";
	str_msg += "As data changes in the SWF 'Last Name', it is updated in the HTML 'Last Name'.";
	alert( str_msg );
	var lastNameCallback = function( event ) {
		// get the object which fired the event
		var swfObj_source = event.getTarget();
		// as the value in the swf 'Last Name' changes, so does the html 'Last Name'
		document.frm_demo.frm_lastName.value = swfObj_source.getText();
		
		return;
	}
	swfObj_lastName.addEventListener( "change", lastNameCallback );
	
	return;
} // ---------- initDemo --------------------------------------

/* ============================================================
	Function:    throwError
	Description: Used to demonstrate the exception handling
	capabilities of the FABridge.
============================================================ */
function throwError() {
	var str_msg = "";
	// call the intentially error-prone 'causeTrouble()' ActionScript function
	// wrapped in a try/catch block, we at least get the error number.
	// if we have the Flash debug player installed, we can also get a description,
	// file name, line number, and stack trace.
	try {
		flexApp.causeTrouble()
	}
	catch( e ) {
		str_msg += "Name: "        + e.name       + "\n\n";
		str_msg += "File Name: "   + e.fileName   + "\n\n";
		str_msg += "Line Number: " + e.lineNumber + "\n\n";
		str_msg += "Message: "     + e.message    + "\n\n";
		str_msg += "Stack: "       + e.stack      + "\n";
		
		alert( str_msg );
	}
	
	return;
} // ---------- throwError --------------------------------------